home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / EGAVGA.SWG / 0005_CIRCLE.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  2KB  |  74 lines

  1. { SC> I had some free time the other day so I decided to play around
  2.  SC> With some Graphics.  I am using TRIG Functions to draw a
  3.  SC> circle.  But it's not too fast.  I understand that using
  4.  SC> Shift operators to multiply and divide will be faster.  But
  5.  SC> am not sure how to do numbers which are not powers of 2.
  6.  SC> Here is the code; how else can we make it faster?
  7.  
  8. Using shifts to multiply things is one way to speed it up but that's difficult
  9. For generic multiplies and only applies to Integer multiplies.  There's an even
  10. faster way to draw a circle if you are interested. <YES he says>  OK, first it
  11. is called the "Bresenham Circle Algorithm" and Uses symmetry about the eight
  12. octants to plot the circle and Uses only Integer arithmetic throughout.  Here
  13. is the code.
  14. }
  15. Uses
  16.   Graph, KASUtils;
  17.  
  18. Var
  19.   Gd, Gm : Integer;
  20.  
  21. Procedure DrawCircle(X, Y, Radius:Word; Color:Byte);
  22. Var
  23.    Xs, Ys    : Integer;
  24.    Da, Db, S : Integer;
  25. begin
  26.      if (Radius = 0) then
  27.           Exit;
  28.  
  29.      if (Radius = 1) then
  30.      begin
  31.           PutPixel(X, Y, Color);
  32.           Exit;
  33.      end;
  34.  
  35.      Xs := 0;
  36.      Ys := Radius;
  37.  
  38.      Repeat
  39.            Da := Sqr(Xs+1) + Sqr(Ys) - Sqr(Radius);
  40.            Db := Sqr(Xs+1) + Sqr(Ys - 1) - Sqr(Radius);
  41.            S  := Da + Db;
  42.  
  43.            Xs := Xs+1;
  44.            if (S > 0) then
  45.                 Ys := Ys - 1;
  46.  
  47.            PutPixel(X+Xs-1, Y-Ys+1, Color);
  48.            PutPixel(X-Xs+1, Y-Ys+1, Color);
  49.            PutPixel(X+Ys-1, Y-Xs+1, Color);
  50.            PutPixel(X-Ys+1, Y-Xs+1, Color);
  51.            PutPixel(X+Xs-1, Y+Ys-1, Color);
  52.            PutPixel(X-Xs+1, Y+Ys-1, Color);
  53.            PutPixel(X+Ys-1, Y+Xs-1, Color);
  54.            PutPixel(X-Ys+1, Y+Xs-1, Color);
  55.      Until (Xs >= Ys);
  56. end;
  57.  
  58. {It Uses Sqr at the moment, but you could code it to use X * X instead of Sqr(X)
  59. if you like since it will probably speed it up.  I haven't had time to optimise
  60. it yet since it will ultimately be in Assembler.
  61.  
  62. Hope this comes in handy For what you're doing. :-) Oh BTW it assumes you have
  63. a PlotDot routine which takes the obvious parameters.
  64. }
  65.  
  66. begin
  67.   EGAVGA_Exe;
  68.   gd := detect;
  69.   InitGraph(gd,gm,'');
  70.   clearviewport;
  71.  
  72.   drawcircle(100,100,150,yellow);
  73.   readln;
  74. end.